package org.liuqm.servlet;


import org.liuqm.pojo.Address;
import org.liuqm.pojo.SysUser;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class RequestToPojo <T>{


    public static <T>T convertToPojo(Map<String,String[]> parameterMap,Class<T> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

       T t=clazz.newInstance();

       //从request中获取参数赋值给实例对象

        //1、从reuqest中获取所有的参数
        //2、迭代map取出key对应实体对象的属性名，value对应属性值
        //
        //
        // 赋值给实体对象  通过setter 方法赋值




        return t;


    }


    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Map<String,String[]>  parameterMap=new HashMap<>();
        parameterMap.put("realname",new String[] {"张三"});

        SysUser sysUser= RequestToPojo.convertToPojo(parameterMap, SysUser.class);

        System.out.println(sysUser.getRealname());

        Map<String,String[]>  parameterMap1=new HashMap<>();
        parameterMap1.put("contact",new String[] {"李四"});

        Address address= RequestToPojo.convertToPojo(parameterMap1, Address.class);

        System.out.println(address.getContact());


    }
}
